package vashion.azeroth.core.api.service.impl;

import java.util.ArrayList;
import java.util.List;

import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;

import org.springframework.stereotype.Service;

import vashion.azeroth.contant.GenericErrorEnum;
import vashion.azeroth.core.api.request.AddItemRequest;
import vashion.azeroth.core.api.request.Sku;
import vashion.azeroth.core.api.response.AddItemResponse;
import vashion.azeroth.core.api.rest.HttpRestRequest;
import vashion.azeroth.core.api.service.ApiItemService;
import vashion.azeroth.core.oss.MoneyUtil;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;

@Service
public class ApiItemServiceImpl implements ApiItemService {

	private AddItemResponse buidErrorAddItemResponse(String errorCode,
			String errorDesc) {

		AddItemResponse aor = new AddItemResponse();

		aor.setSuccess(false);

		aor.setErrorCode(errorCode);

		aor.setErrorDesc(errorDesc);

		return aor;
	}

	@Override
	public AddItemResponse addItem(AddItemRequest addItemRequest) {

		if (!addItemRequest.check()) {

			return buidErrorAddItemResponse(
					GenericErrorEnum.PARAMETER_ERROR.getErrorMessage(),
					GenericErrorEnum.PARAMETER_ERROR.getErrorMessage());
		}

		try {
			// 将参数转换为JSON格式，用于REST请求
			String request = addItemRequest.buildJson();

			// 通过HTTP请求
			String res = HttpRestRequest.exec(request);

			// 将返回参数转换为对象
			AddItemResponse aor = JSON.parseObject(res, AddItemResponse.class);

			return aor;

		} catch (Exception e) {

			return buidErrorAddItemResponse(
					GenericErrorEnum.SYSTEM_ERROR.getErrorMessage(),
					GenericErrorEnum.SYSTEM_ERROR.getErrorMessage());

		}

	}

	public static void main(String[] args) {

		ApiItemService apiItemService = new ApiItemServiceImpl();

		AddItemRequest addItemRequest = new AddItemRequest();

		addItemRequest.setCategoryCode("ategoryCodeTest");
		addItemRequest.setSalesPrice(789D);

		/**
		 * 商品代码
		 * 
		 * */
		@NonNull
		String code = "azeroth test";
		addItemRequest.setCode(code);

		/**
		 * skus规格数组
		 * 
		 * */
		List<Sku> skus = new ArrayList<Sku>();

		Sku sku = new Sku();

		/**
		 * 规格代码
		 * */
		@NonNull
		String skuCode = "sku code test";
		sku.setSkuCode(skuCode);

		/**
		 * 规格名称
		 * */
		@NonNull
		String skuName = "sku name test";
		sku.setSkuName(skuName);
		
        sku.setSkuSalesPrice("78");


		skus.add(sku);
		
		Sku sku1 = new Sku();
		sku1.setSkuCode("77777");
		sku1.setSkuName("88888");	
        sku1.setSkuSalesPrice("78");

		skus.add(sku1);

		
		

		addItemRequest.setSkus(skus);

		addItemRequest.setName("azeroth test");

		AddItemResponse response = apiItemService.addItem(addItemRequest);

		System.out.println(JSON.toJSONString(response, true));

	}

}
